angular.module('tab-makeorder', [])
  .controller 'TabMakeorderCtrl', ($localStorage,Modal,wfConfig,$location,$scope,$http, $state,Toast,$filter,$window, $ionicHistory,ionicDatePicker,datepickerBaseConfig,$timeout) ->

    $scope.commit={};
    $scope.proList=[];
    $scope.commit.startTime=new Date()
    $scope.commit.endTime=new Date()
    $scope.commit.proName="0"
    $scope.commit.shift="0"
    console.log($localStorage.crew.ownerUnitId);
    #返回
    $scope.back= ->
      window.history.back()
    #编辑个人资料
    $scope.toEdit= ->
      $scope.editing=true;
    #开始时间选择
    $scope.openDatePickerS = ->
      config = datepickerBaseConfig(new Date(), Date.parse($scope.commit.startTime))
      config.callback = (val)->
        if val
          $scope.commit.startTime = $filter('date')(new Date(val), 'yyyy-MM-dd')
      ionicDatePicker.openDatePicker(config);
    #结束时间选择
    $scope.openDatePickerE = ->
      config = datepickerBaseConfig(new Date(), Date.parse($scope.commit.endTime))
      config.callback = (val)->
        if val
          $scope.commit.endTime = $filter('date')(new Date(val), 'yyyy-MM-dd')
      ionicDatePicker.openDatePicker(config);
    #获取服务项目
    getProductInfo = ->
      $http.get('/product', params: {unitId: $localStorage.crew.ownerUnitId})
      .success (resp)->
        if resp.code == 0
          $scope.proList=resp.message.content
          console.log($scope.proList[0].id);
        else
          Toast.showMsg("获取数据失败")
      .error (resp, status) ->
        console.log(resp)
        Toast.showMsg("加载数据失败")
    getProductInfo()


    #提交订单资料
    $scope.commitEdit= ->
      $scope.deteCheck=Date.parse($scope.commit.endTime)-Date.parse($scope.commit.startTime);
      if $scope.commit.proName=="0"
        Toast.showMsg("请选择服务项目")
        return false
      console.log(typeof $scope.commit.ctName);
      if typeof $scope.commit.ctName=="undefined" ||$scope.commit.ctName==""||$scope.commit.ctName==null
        Toast.showMsg("请输入客户名称")
        return false
      if typeof $scope.commit.address=="undefined" || $scope.commit.address==""||$scope.commit.address==null
        Toast.showMsg("请输入床位信息")
        return false
      if typeof $scope.commit.tel=="undefined" || $scope.commit.tel==""||$scope.commit.tel==null
        Toast.showMsg("请输入电话")
        return false
      if typeof $scope.commit.charge=="undefined" || $scope.commit.charge==""||$scope.commit.charge==null
        Toast.showMsg("请输入总金额")
        return false
      if $scope.deteCheck<=0
        Toast.showMsg("请选择正确的天数")
        return false

      $http.post('/order/valet',{
        bedNo: $scope.commit.address
        charge: $scope.commit.charge
        customerName:$scope.commit.ctName
        start:$filter('date')(new Date($scope.commit.startTime), 'yyyy-MM-dd')
        discount: $scope.commit.count
        tel: $scope.commit.tel
        end: $scope.commit.endTime
        phone: $scope.commit.tel
        productId: $scope.commit.proName
        shift:$scope.commit.shift
      })
        .success (resp) ->
          if resp.code == 0
            $window.location.reload();
            Toast.showMsg("提交订单成功")
          else
            Toast.showMsg("添加数据失败")
        .error (resp, status) ->
          console.log(resp)
          Toast.showMsg("添加数据失败")



